<?php
namespace App\Services\Core\Store\Moxie;
use Requests;

class MoxieService {

    public static function get($url, $apiName){


        $headers = array(
            'Content-Type' => 'application/json',
            'Authorization' => 'token ' . MoxieConfig::MOXIE_WEBHOOK_TOKEN);

        $return = Requests::get($url, $headers);
        $status = $return->status_code;
        if($status == 200){
            return $return->body;
        } else {
            self::error($status, $return->body, $apiName);
        }

        return null;
    }

    private static function error($status, $body, $apiName){

        $bodyObj = json_decode($body, true);
        if(array_key_exists("message", $bodyObj)){
            echo sprintf("%s, status:%s, message:%s\n", $apiName, $status, $bodyObj['message']);
        }else if(array_key_exists("detail", $bodyObj)){
            echo sprintf("%s, status:%s, detail:%s\n", $apiName, $status, $bodyObj['detail']);
        }
    }




}
